package com.guodi.map.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guodi.map.entity.MapFunctionParam;
import com.guodi.map.entity.MapFunctionServer;
import com.guodi.map.mapper.MapFunctionServerMapper;
import com.guodi.map.service.IMapFunctionParamService;
import com.guodi.map.service.IMapFunctionServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @描述：功能服务表 服务类
 * @作者: 代鹏刚
 * @日期: 2019-12-26
 */
@Service
public class MapFunctionServerServiceImpl extends ServiceImpl<MapFunctionServerMapper, MapFunctionServer> implements IMapFunctionServerService {

    @Resource
    private MapFunctionServerMapper mapFunctionServerMapper;

    @Autowired
    private IMapFunctionParamService iMapFunctionParamService;

    @Override
    public List<MapFunctionServer> listByEntity(Page page, String keyWord,  List<String> orgIds, Integer isShare) {
        return mapFunctionServerMapper.listByEntity(page, keyWord, orgIds, isShare);
    }

    @Override
    public void updateIsShare(String[] ids, Integer isShare) {
        mapFunctionServerMapper.updateIsShare(ids,isShare);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(MapFunctionServer mapFunctionServer, List<MapFunctionParam> paramList ) {

        this.updateById(mapFunctionServer);
        LambdaQueryWrapper<MapFunctionParam> queryWrapper = (new QueryWrapper()).lambda();
        queryWrapper.eq(MapFunctionParam::getFunServerId,mapFunctionServer.getId());
        iMapFunctionParamService.remove(queryWrapper);
        if(!paramList.isEmpty()){
            iMapFunctionParamService.saveBatch(paramList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(MapFunctionServer mapFunctionServer, List<MapFunctionParam> paramList) {
        this.save(mapFunctionServer);
        iMapFunctionParamService.saveBatch(paramList);
    }
}